<?php
/*
	路径生成器
	提供文件或文件夹的存储路径
	更新记录：
		2016-04-12 创建
*/
class PathBuilder
{	

	function __construct() 
	{
	}
	
	/*
		返回上传路径：
		示例：dz\data\attachment\forum\
	*/
	function getRoot()
	{
        //定位到/config/
        $root = str_replace("\\", "/", __FILE__);
        $pos = strpos($root,"/source/plugin/up6/db/biz/PathBuilder.php");        
        //
        $root = substr($root, 0,$pos+1);

		$cr = new ConfigReader();
		$pathSvr = $cr->readString("IO")["dir"];
		$pathSvr = str_replace("{root}",$root,$pathSvr);
		//source/plugin/up6
		$pathSvr = str_replace("\\","/",$pathSvr);
		//附件路径=>dz/source/data/attachment/forum/
		return PathTool::combin($pathSvr,"data/attachment/forum/");
	}
	
	function getRootDz()
	{
		$file_path = dirname(__FILE__);//D:\wamp\www\dz2.5_utf8\source\plugin\up6\db\biz
		$file_path .= "\\..\\..\\..\\..\\data\\attachment\\forum\\";
		//$file_url  = $_SERVER["PHP_SELF"];///dz2.5_utf8/source/plugin/up6/db/test.php
		//$file_path 	= str_replace("\\", "/", $file_path);//转换路径，支持Linux
		return realpath($file_path . "/../../../../data/attachment/forum/");
	}
	
	function genFolder($uid,&$fd){}

	/**
	 * data/attachment/forum/2023/10/18/md5.attach
	 */
	function genFile($file)
	{
		date_default_timezone_set("PRC");//设置北京时区
		$path = $this->getRoot();
		$path = PathTool::combin($path, date("Y"));
		$path = PathTool::combin($path, date("m"));
		$path = PathTool::combin($path, date("d"));
		
		if(!is_dir($path)) mkdir($path,0777,true);
		$path = realpath($path);//规范化路径
		$path = PathTool::combin($path, $file->md5);
		$path .= ".";
		$path .= "attach";//增强安全性，防止用户上传php脚本
		return $path;
	}
	
	function genRelPath($file)
	{
		date_default_timezone_set("PRC");//设置北京时区
		$pathRel = "";
		//
		$pathRel = date("Y");
		$pathRel = PathTool::combin($pathRel,date("m"));
		$pathRel = PathTool::combin($pathRel,date("d"));
		$pathRel = PathTool::combin($pathRel,$file->md5);
		$pathRel = $pathRel . ".attach";
		return $pathRel;
	}
}
?>